/*Start From Here*/

$(function () {

    // TODO

});


/*Row Styles For Case Table*/
function rowStyle(row, index) {
    /*define five type*/
    var classes = ['active', 'success', 'info', 'warning', 'danger'];

    /*if has error*/
    if (row["failCount"] > 0) {
        return {
            classes: classes[4]
        };
    }
    /*if no error*/
    else if (row["failCount"] == 0)
    {
        return {
            classes: classes[1]
        };
    }
    /*for old data --- no fail column*/
    else if (row["failCount"] == "" || row["failCount"] == null)
    {
        return{
            classes: classes[3]
        }
    }
    /*default*/
    return {};
}


function detailFormatter(index, row)
{
    var result = "";
    $.each(row, function (key, value)
    {
        if(key == 'header')
        {
            result = "<b>Response Header:</b>" + "\n" + value;
        }
        if(key == 'body')
        {
            var sJSON = value;
            if (sJSON.length > 0)
            {
                try
                {
                    var oJSON = JSON.parse(sJSON);

                    result = result + "<b>Body:</b>" + "\n" + FormatJSON(oJSON);
                }
                catch(sError)
                {
                    /*if NOT json format*/
                    result = result + "<b>Body:</b>" + "\n" + value;
                }
            }
            else
            {
                alert("NULL");
            }
        }
    });
    return result;
}

function FormatJSON(oData, sIndent) {
    if (arguments.length < 2) {
        var sIndent = "";
    }
    var sIndentStyle = "    ";
    var sDataType = RealTypeOf(oData);

    // open object
    if (sDataType == "array") {
        if (oData.length == 0) {
            return "[]";
        }
        var sHTML = "[";
    } else {
        var iCount = 0;
        $.each(oData, function() {
            iCount++;
            return;
        });
        if (iCount == 0) {
            // object is empty
            return "{}";
        }
        var sHTML = "{";
    }

    // loop through items
    var iCount = 0;
    $.each(oData, function(sKey, vValue) {
            if (iCount > 0) {
                sHTML += ",";
            }
            if (sDataType == "array") {
                sHTML += ("\n" + sIndent + sIndentStyle);
            } else {
                sHTML += ("\n" + sIndent + sIndentStyle + "\"" + sKey + "\"" + ": ");
            }

            // display relevant data type
            switch (RealTypeOf(vValue)) {
                case "array":
                case "object":
                    sHTML += FormatJSON(vValue, (sIndent + sIndentStyle));
                    break;
                case "boolean":
                case "number":
                    sHTML += vValue.toString();
                    break;
                case "null":
                    sHTML += "null";
                    break;
                case "string":
                    sHTML += ("\"" + vValue + "\"");
                    break;
                default:
                    sHTML += ("TYPEOF: " + typeof(vValue));
            }
            // loop
            iCount++;
        }
    )

    // close object
    if (sDataType == "array") {
        sHTML += ("\n" + sIndent + "]");
    } else {
        sHTML += ("\n" + sIndent + "}");
    }
    // return
    return sHTML;
}

function RealTypeOf(v) {
    if (typeof(v) == "object") {
        if (v === null) return "null";
        if (v.constructor == (new Array).constructor) return "array";
        if (v.constructor == (new Date).constructor) return "date";
        if (v.constructor == (new RegExp).constructor) return "regex";
        return "object";
    }
    return typeof(v);
}